//
//  PlaylistView.m
//  Kugou-OC
//
//  Created by BH on 16/5/12.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "PlaylistView.h"
#import "PlaylistViewCell.h"
#import "GlobalDefine.h"
#import "MusicInfoModel.h"
#import <SDAutoLayout/UIView+SDAutoLayout.h>

@interface PlaylistView (){
    NSMutableArray *indexArr;  //索引数组：0-最近选中 1-当前选中
    NSIndexPath *selectedIndex;  //当前选中索引
}

@end

@implementation PlaylistView

-(id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        indexArr = [NSMutableArray array];
        
        //播放完成后调用
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeRow:) name:@"PlayerItemDidPlayToEndTime" object:nil];
    }
    return self;
}

/**
 *  当前播放完成后切换到下一首
 *
 *  @param info <#info description#>
 */
-(void)changeRow:(NSNotification *)info{
    _currentIndex = [info.object integerValue];
    [self reloadData];
}

-(void)setCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _playList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PlaylistViewCell *cell = [PlaylistViewCell cellWithTableView:tableView];
    
    if (indexPath.row<10) {
        [cell.iconBtn setTitle:[NSString stringWithFormat:@"0%d",(short)indexPath.row+1] forState:UIControlStateNormal];
    }else{
        [cell.iconBtn setTitle:[NSString stringWithFormat:@"%d",(short)indexPath.row+1] forState:UIControlStateNormal];
    }
    
    if (_currentIndex == indexPath.row) {
        cell.singName.textColor = BG_COLOR;
        cell.singer.textColor = BG_COLOR;
        cell.downloadBtn.hidden = NO;
        cell.favoriteBtn.hidden = NO;
        
        [indexArr addObject:indexPath];
    }else{
        cell.singName.textColor = TITLE_ONE_TEXT_COLOR;
        cell.singer.textColor = TITLE_SECOND_TEXT_COLOR;
        cell.downloadBtn.hidden = YES;
        cell.favoriteBtn.hidden = YES;
    }
    
    cell.singName.text = [_playList[indexPath.row] music_name];
    cell.singer.text = [_playList[indexPath.row] music_artist];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    sectionV.backgroundColor = [UIColor whiteColor];
    //播放队列
    UILabel *leftLab = ALLOC(UILabel);
    leftLab.textColor = BG_COLOR;
    NSMutableAttributedString *mutableStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"播放队列 %d",(short)_playList.count]];
    [mutableStr addAttribute:NSFontAttributeName value:TITLE_THIRD_FONT range:NSMakeRange(0, 4)];
    [mutableStr addAttribute:NSFontAttributeName value:TITLE_FIVE_FONT range:NSMakeRange(5, mutableStr.length-5)];
    leftLab.attributedText = mutableStr;
    //删除
    UIButton *deleteBtn = ALLOC(UIButton);
    [deleteBtn setImage:IMAGE(@"playlist_deleteall_btn") forState:UIControlStateNormal];
    //播放模式
    UIButton *modeBtn = ALLOC(UIButton);
    [modeBtn setImage:IMAGE(@"playlist_mode_serious") forState:UIControlStateNormal];
    //添加歌单
    UIButton *addListBtn = ALLOC(UIButton);
    [addListBtn setImage:IMAGE(@"playlist_addMusicList") forState:UIControlStateNormal];
    //分割线
    UIImageView *line1 = ALLOC(UIImageView);
    line1.image = IMAGE(@"playlist_seprateline_vertical_short");
    UIImageView *line2 = ALLOC(UIImageView);
    line2.image = IMAGE(@"playlist_seprateline_vertical_short");
    
    [sectionV sd_addSubviews:@[leftLab,addListBtn,line1,modeBtn,line2,deleteBtn]];
    
    leftLab.sd_layout.leftSpaceToView(sectionV,15).topEqualToView(sectionV).heightIs(sectionV.height).widthIs(120);
    deleteBtn.sd_layout.rightSpaceToView(sectionV,0).topSpaceToView(sectionV,0).heightIs(44).widthEqualToHeight();
    line2.sd_layout.rightSpaceToView(deleteBtn,0).widthIs(1).topSpaceToView(sectionV,10).bottomSpaceToView(sectionV,10);
    modeBtn.sd_layout.rightSpaceToView(line2,0).topEqualToView(deleteBtn).bottomEqualToView(deleteBtn).widthIs(deleteBtn.width);
    line1.sd_layout.rightSpaceToView(modeBtn,0).topEqualToView(line2).bottomEqualToView(line2).widthIs(1);
    addListBtn.sd_layout.rightSpaceToView(line1,0).topEqualToView(modeBtn).bottomEqualToView(modeBtn).widthIs(modeBtn.width);
    
    return sectionV;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath *lastIndex;
    
    if (indexArr.count==2) {
        //替换索引
        lastIndex = [indexArr lastObject];
        selectedIndex = indexPath;
        indexArr[0] = lastIndex;
        indexArr[1] = selectedIndex;
    }else{
        //添加2个对象：上次选中、当前选中
        [indexArr addObject:indexPath];
    }
    //更改cell样式
    PlaylistViewCell *lastCell = [tableView cellForRowAtIndexPath:[indexArr firstObject]];
    lastCell.singName.textColor = [UIColor blackColor];
    lastCell.singer.textColor = [UIColor darkGrayColor];
    lastCell.downloadBtn.hidden = YES;
    lastCell.favoriteBtn.hidden = YES;
    
    PlaylistViewCell *selectCell = [tableView cellForRowAtIndexPath:indexPath];
    selectCell.singName.textColor = BG_COLOR;
    selectCell.singer.textColor = BG_COLOR;
    selectCell.downloadBtn.hidden = NO;
    selectCell.favoriteBtn.hidden = NO;
    
    if (self.Play) {
        self.Play(_playList,indexPath.row);
    }
}

@end
